﻿#region Disclaimer/Info
///////////////////////////////////////////////////////////////////////////////////////////////////
// MetaCollection
// 
// MetaCollection is a system to provide a standard structure, permissions and metadata service
// for content organization.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection/
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using MetaCollection.Framework.Change;
using MetaCollection.Framework.Model;
using System.Globalization;
using System.Linq;


namespace MetaCollection.Framework.Repository
{
	internal class DistributedModelRepository : DistributedRepository<IModelRepository>, IModelRepository
	{
		#region IModelRepository Members

		/// <summary>
		/// Find the provider by Id
		/// </summary>
		/// <param name="id">The id of the provider.</param>
		/// <returns>
		/// An IProviderType record for the given Id or null if no object found
		/// </returns>
		public IItemSpecification FindSpecification(Guid id)
		{
			string key = ReferenceKey(id, null);
			return ResolveReference(key).Select((i) => i.FindSpecification(id)).FirstOrDefault();
		}

		/// <summary>
		/// Finds the resource provider type configuration record given a specific resource type name
		/// </summary>
		/// <param name="name">the name of the resource type handler</param>
		/// <returns>a resource type record</returns>
		public IItemSpecification FindSpecificationByName(string name)
		{
			string key = ReferenceKey(Guid.Empty, name);
			return ResolveReference(key).Select((i) => i.FindSpecificationByName(name)).FirstOrDefault();

		}

		/// <summary>
		/// Puts the model Specification into persistant storage or updates an existing model specification.
		/// </summary>
		/// <param name="item"></param>
		public void SaveSpecification(IItemSpecification item)
		{
			DefaultRepository.SaveSpecification(item);
		}

		/// <summary>
		/// Finds all registered item specification models in the repository
		/// </summary>
		/// <value></value>
		/// <returns>A collection IItemSpecifications.</returns>
		public IEnumerable<IItemSpecification> RegisteredSpecifications
		{
			get {
				return Enumerable.SelectMany(ResolveReference(null).Select((r) => r.RegisteredSpecifications), (x) => x.Select(a => a));
			}
		}

		#endregion
	}
}
